package com.qzsoft.zhjd.base.modules.parking.service.impl;

import java.util.HashMap;
import java.util.Map;

import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.base.modules.parking.dto.BaseParkingDto;
import com.qzsoft.zhjd.base.modules.parking.dto.BaseParkingReceiveDto;
import com.qzsoft.zhjd.base.modules.parking.entity.BaseParkingEntity;
import com.qzsoft.zhjd.base.modules.parking.mapper.BaseParkingMapper;
import com.qzsoft.zhjd.base.modules.parking.service.BaseParkingService;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;

/**
 * @ClassName BaseParkingServiceImpl
 * @Description 停车信息业务接口实现
 * @author linhuawei
 * @since 2021-12-16
 */
@Service
public class BaseParkingServiceImpl extends ServiceImpl<BaseParkingMapper, BaseParkingEntity>
        implements BaseParkingService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @ZhjdDataFilter(tableAlias = "t1")
    @Override
    public IPage<BaseParkingDto> page(Map<String, Object> params) {
        if (null == params) {
            params = new HashMap<String, Object>();
        }
        IPage<BaseParkingDto> page = baseServiceComponent.getPage(params, BaseParkingDto.class);
        return baseMapper.queryDto(page, params);
    }

    @Override
    public void save(String baseCode, BaseParkingReceiveDto dto) {
        // 删除已经存在的数据
        QueryWrapper<BaseParkingEntity> queryWrapper = new QueryWrapper<BaseParkingEntity>();
        queryWrapper.eq("base_code", baseCode);
        queryWrapper.eq("original_id", dto.getOriginalId());
        baseMapper.delete(queryWrapper);

        // 添加新的数据
        BaseParkingEntity entity = dto.coverToEntity();
        entity.setBaseCode(baseCode);
        baseMapper.insert(entity);
    }

}
